package com.ewanghuiju.app.ui.my.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ewanghuiju.app.R;
import com.ewanghuiju.app.app.App;
import com.ewanghuiju.app.app.Constants;
import com.ewanghuiju.app.base.BaseActivity;
import com.ewanghuiju.app.base.contract.my.ChangeBodyWithCodeContract;
import com.ewanghuiju.app.component.RxBus;
import com.ewanghuiju.app.model.bean.request.PhoneRegistbindRequestBean;
import com.ewanghuiju.app.model.bean.response.UserInfoResponBean;
import com.ewanghuiju.app.model.dp.RealmHelper;
import com.ewanghuiju.app.model.event.SendEvent;
import com.ewanghuiju.app.presenter.my.ChangeBodyWithCodePresenter;
import com.ewanghuiju.app.util.LoadingDialogUtils;
import com.ewanghuiju.app.util.MD5Util;
import com.ewanghuiju.app.util.StringUtil;
import com.ewanghuiju.app.util.nodoubleclick.AntiShake;
import com.ewanghuiju.app.widget.EditTextWithDel;
import com.gyf.immersionbar.ImmersionBar;
import com.muddzdev.styleabletoast.StyleableToast;

import androidx.core.content.ContextCompat;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author sj  qq:751943509
 * @class 修改信息  用到手机验证码页面
 * @time 2020/2/24
 */
public class ChangeBodyWithCodeActivity extends BaseActivity<ChangeBodyWithCodePresenter> implements ChangeBodyWithCodeContract.View {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.et_bind_zfb_account)
    EditTextWithDel etBindZfbAccount;
    @BindView(R.id.et_zfb_account_name)
    EditTextWithDel etZfbAccountName;
    @BindView(R.id.et_zfb_phone_no)
    EditTextWithDel etZfbPhoneNo;
    @BindView(R.id.et_input_zfb_code)
    EditTextWithDel etInputZfbCode;
    @BindView(R.id.tv_zfb_code_text)
    TextView tvZfbCodeText;
    @BindView(R.id.layout_bind_zfb)
    LinearLayout layoutBindZfb;
    @BindView(R.id.et_change_phone_input_pwd)
    EditTextWithDel etChangePhoneInputPwd;
    @BindView(R.id.iv_change_phone_eye)
    ImageView ivChangePhoneEye;
    @BindView(R.id.tv_yuan_phone_no)
    TextView tvYuanPhoneNo;
    @BindView(R.id.et_change_phone_input_new)
    EditTextWithDel etChangePhoneInputNew;
    @BindView(R.id.et_change_phone_input_code)
    EditTextWithDel etChangePhoneInputCode;
    @BindView(R.id.layout_change_phone)
    LinearLayout layoutChangePhone;
    @BindView(R.id.tv_change_pwd_phone_no)
    TextView tvChangePwdPhoneNo;
    @BindView(R.id.et_input_change_pwd_code)
    EditTextWithDel etInputChangePwdCode;
    @BindView(R.id.tv_change_pwd_code_text)
    TextView tvChangePwdCodeText;
    @BindView(R.id.et_change_pwd_new_pwd)
    EditTextWithDel etChangePwdNewPwd;
    @BindView(R.id.et_change_pwd_sure_pwd)
    EditTextWithDel etChangePwdSurePwd;
    @BindView(R.id.layout_change_pwd)
    LinearLayout layoutChangePwd;
    @BindView(R.id.tv_setting_paypwd_phone_no)
    TextView tvSettingPaypwdPhoneNo;
    @BindView(R.id.et_input_setting_paypwd_paypwd)
    EditTextWithDel etInputSettingPaypwdPaypwd;
    @BindView(R.id.et_setting_paypwd_sure_pwd)
    EditTextWithDel etSettingPaypwdSurePwd;
    @BindView(R.id.layout_setting_paypwd)
    LinearLayout layoutSettingPaypwd;
    @BindView(R.id.tv_sure_change)
    TextView tvSureChange;
    @BindView(R.id.tv_change_phone_getcode)
    TextView tvChangePhoneGetcode;
    private int mType;

    private UserInfoResponBean userInfoResponBean;

    private RealmHelper realmHelper;

    @Override
    protected int getLayout() {
        return R.layout.activity_change_boywithcode;
    }

    @Override
    protected void initEventAndData() {
        mType = getIntent().getIntExtra(Constants.SETTING_TYPE, 1);

        realmHelper = App.getAppComponent().realmHelper();
        userInfoResponBean = realmHelper.getUserInfoResponBean();
        if (mType == 1) {
            tvTitle.setText(R.string.setting_nickname_set);
        } else if (mType == 2) {
            if (userInfoResponBean != null) {
                tvSettingPaypwdPhoneNo.setText(StringUtil.getFormatePhone(userInfoResponBean.getMobile()));
            }
            tvTitle.setText(R.string.setting_pay_pw);
            layoutSettingPaypwd.setVisibility(View.VISIBLE);
        } else if (mType == 3) {
            //绑定支付宝
            tvTitle.setText(R.string.setting_bind_zfb);
            layoutBindZfb.setVisibility(View.VISIBLE);
            if (userInfoResponBean != null) {
                etZfbPhoneNo.setText(StringUtil.getFormatePhone(userInfoResponBean.getMobile()));
            }
        } else if (mType == 4) {
            //修改手机号
            tvTitle.setText(R.string.setting_change_phone);
            layoutChangePhone.setVisibility(View.VISIBLE);
            if (userInfoResponBean != null) {
                tvYuanPhoneNo.setText(StringUtil.getFormatePhone(userInfoResponBean.getMobile()));
            }
        } else if (mType == 5) {
            //修改登录密码
            tvTitle.setText(R.string.setting_change_login_pwd);
            layoutChangePwd.setVisibility(View.VISIBLE);
            if (userInfoResponBean != null) {
                tvChangePwdPhoneNo.setText(StringUtil.getFormatePhone(userInfoResponBean.getMobile()));
            }
        }
    }

    @OnClick({R.id.tv_sure_change, R.id.tv_zfb_code_text, R.id.tv_change_phone_getcode, R.id.tv_change_pwd_code_text})
    public void doClick(View view) {
        if (AntiShake.check(view.getId())) {    //判断是否多次点击
            return;
        }
        switch (view.getId()) {
            case R.id.tv_sure_change:
                if (mType == 1) {

                } else if (mType == 2) {
                    //设置支付密码
                    setPayPwd();
                } else if (mType == 3) {
                    //绑定支付宝
                    bindZfb();
                } else if (mType == 4) {
                    //修改手机号
                    changePhoneNumber();
                } else if (mType == 5) {
                    //修改登录密码
                    changeLoginPwd();
                }
                break;
            case R.id.tv_zfb_code_text:
            case R.id.tv_change_phone_getcode:
            case R.id.tv_change_pwd_code_text:
                //获取验证码
                if (userInfoResponBean == null || TextUtils.isEmpty(userInfoResponBean.getMobile())) {
                    StyleableToast.makeText(mContext, "未获取到手机号", Toast.LENGTH_SHORT, R.style.mytoast).show();
                    return;
                }
                LoadingDialogUtils.show(mContext);
                mPresenter.getCode(userInfoResponBean.getMobile());
                break;
            default:
                break;
        }
    }

    /**
     * 修改登录密码
     */
    private void changeLoginPwd() {
        if (userInfoResponBean == null || TextUtils.isEmpty(userInfoResponBean.getMobile())) {
            StyleableToast.makeText(mContext, "未获取到手机号", Toast.LENGTH_SHORT, R.style.mytoast).show();
            return;
        }
        String changePwdCode = etInputChangePwdCode.getText().toString();
        if (TextUtils.isEmpty(changePwdCode)) {
            StyleableToast.makeText(mContext, getString(R.string.change_sure_input_code), Toast.LENGTH_SHORT, R.style.mytoast).show();
            return;
        }
        String changePwdNewPwd = etChangePwdNewPwd.getText().toString();
        if (TextUtils.isEmpty(changePwdNewPwd)) {
            StyleableToast.makeText(mContext, getString(R.string.change_pwd_input_new_pwd), Toast.LENGTH_SHORT, R.style.mytoast).show();
            return;
        }

        String changePwdSurePwd = etChangePwdSurePwd.getText().toString();
        if (TextUtils.isEmpty(changePwdSurePwd)) {
            StyleableToast.makeText(mContext, getString(R.string.change_pwd_input_new_pwd), Toast.LENGTH_SHORT, R.style.mytoast).show();
            return;
        }

        if (!changePwdNewPwd.equals(changePwdSurePwd)) {
            StyleableToast.makeText(mContext, "两次输入密码不一致", Toast.LENGTH_SHORT, R.style.mytoast).show();
            return;
        }

        LoadingDialogUtils.show(mContext);
        PhoneRegistbindRequestBean phoneRegistbindRequestBean = new PhoneRegistbindRequestBean();
        phoneRegistbindRequestBean.setMobile(userInfoResponBean.getMobile());
        phoneRegistbindRequestBean.setCode(changePwdCode);
        phoneRegistbindRequestBean.setPwd(changePwdNewPwd);
        phoneRegistbindRequestBean.setPwd1(changePwdNewPwd);
        mPresenter.editpwd(phoneRegistbindRequestBean);
    }

    /**
     * 修改手机号
     */
    private void changePhoneNumber() {
        if (userInfoResponBean == null || TextUtils.isEmpty(userInfoResponBean.getMobile())) {
            StyleableToast.makeText(mContext, "未获取到原手机号", Toast.LENGTH_SHORT, R.style.mytoast).show();
            return;
        }
        String phoneInputPwd = etChangePhoneInputPwd.getText().toString();
        if (TextUtils.isEmpty(phoneInputPwd)) {
            StyleableToast.makeText(mContext, getString(R.string.change_input_login_pwd), Toast.LENGTH_SHORT, R.style.mytoast).show();
            return;
        }

        String changePhoneInputNew = etChangePhoneInputNew.getText().toString();
        if (TextUtils.isEmpty(changePhoneInputNew)) {
            StyleableToast.makeText(mContext, getString(R.string.change_input_phone_no), Toast.LENGTH_SHORT, R.style.mytoast).show();
            return;
        }

        String changePhoneInputCode = etChangePhoneInputCode.getText().toString();
        if (TextUtils.isEmpty(changePhoneInputCode)) {
            StyleableToast.makeText(mContext, getString(R.string.change_sure_input_code), Toast.LENGTH_SHORT, R.style.mytoast).show();
            return;
        }
        LoadingDialogUtils.show(mContext);
        mPresenter.mobileEdit(userInfoResponBean.getMobile(), phoneInputPwd, changePhoneInputNew, changePhoneInputCode);

    }

    /**
     * 绑定支付宝
     */
    private void bindZfb() {
        String zfbAccount = etBindZfbAccount.getText().toString();
        if (TextUtils.isEmpty(zfbAccount)) {
            StyleableToast.makeText(mContext, "请输入支付宝账号", Toast.LENGTH_SHORT, R.style.mytoast).show();
            return;
        }
        String zfbName = etZfbAccountName.getText().toString();
        if (TextUtils.isEmpty(zfbName)) {
            StyleableToast.makeText(mContext, "请输入姓名", Toast.LENGTH_SHORT, R.style.mytoast).show();
            return;
        }

        String zfbCode = etInputZfbCode.getText().toString();
        if (TextUtils.isEmpty(zfbCode)) {
            StyleableToast.makeText(mContext, "请输入验证码", Toast.LENGTH_SHORT, R.style.mytoast).show();
            return;
        }
        //绑定支付宝
        LoadingDialogUtils.show(mContext);
        mPresenter.bindAlipay(zfbName, zfbAccount, userInfoResponBean.getMobile(), zfbCode);
    }

    /**
     * 设置支付密码
     */
    public void setPayPwd() {
        String payPwd = etInputSettingPaypwdPaypwd.getText().toString();
        String surePayPwd = etSettingPaypwdSurePwd.getText().toString();
        if (TextUtils.isEmpty(payPwd)) {
            StyleableToast.makeText(mContext, "请输入支付密码", Toast.LENGTH_SHORT, R.style.mytoast).show();
            return;
        }
        if (!StringUtil.isInteger(payPwd)) {
            StyleableToast.makeText(mContext, "支付密码必须为6位数字", Toast.LENGTH_SHORT, R.style.mytoast).show();
            return;
        }
        if (TextUtils.isEmpty(surePayPwd)) {
            StyleableToast.makeText(mContext, "请再次输入支付密码", Toast.LENGTH_SHORT, R.style.mytoast).show();
            return;
        }
        if (!payPwd.equals(surePayPwd)) {
            StyleableToast.makeText(mContext, "两次输入密码不一致", Toast.LENGTH_SHORT, R.style.mytoast).show();
            return;
        }
        LoadingDialogUtils.show(mContext);
        String payPwdMd5 = MD5Util.getMD5(MD5Util.getMD5(payPwd));
        mPresenter.setPayPassword(payPwdMd5);
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this)
                .titleBar(mToolbar, true)
                .statusBarDarkFont(true)
                .init();
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    public void showPayPassword() {
        if (mType == 1) {

        } else if (mType == 2) {
            StyleableToast.makeText(mContext, "设置成功", Toast.LENGTH_SHORT, R.style.mytoast).show();
//            if (userInfoResponBean != null) {
//                userInfoResponBean.setIs_password(1);
//                realmHelper.insertUserInfoResponBean(userInfoResponBean);
//            }
//            RxBus.getDefault().post(new SendEvent("", Constants.TYPE_CHANGE_PAYPWD));
        }
        RxBus.getDefault().post(new SendEvent("", Constants.TYPE_GET_USERINFO));
        onBackPressedSupport();
    }

    @Override
    public void showPayPasswordError(int code, String message) {
        StyleableToast.makeText(mContext, TextUtils.isEmpty(message) ? Constants.ERROR_MESSAGE : message, Toast.LENGTH_SHORT, R.style.mytoast).show();
    }

    @Override
    public void showCodeText(String text) {
        if (mType == 3) {
            if (tvZfbCodeText != null) {
                tvZfbCodeText.setText(text);
            }
        } else if (mType == 4) {
            if (tvChangePhoneGetcode != null) {
                tvChangePhoneGetcode.setText(text);
            }
        } else if (mType == 5) {
            if (tvChangePwdCodeText != null) {
                tvChangePwdCodeText.setText(text);
            }
        }
    }

    @Override
    public void showCodeComplete() {
        if (mType == 3) {
            if (tvZfbCodeText != null) {
                tvZfbCodeText.setText("获取验证码");
                tvZfbCodeText.setClickable(true);
                tvZfbCodeText.setTextColor(ContextCompat.getColor(mContext, R.color.text_primary));
            }
        } else if (mType == 4) {
            if (tvChangePhoneGetcode != null) {
                tvChangePhoneGetcode.setText("获取验证码");
                tvChangePhoneGetcode.setClickable(true);
                tvChangePhoneGetcode.setTextColor(ContextCompat.getColor(mContext, R.color.text_primary));
            }
        } else if (mType == 5) {
            if (tvChangePwdCodeText != null) {
                tvChangePwdCodeText.setText("获取验证码");
                tvChangePwdCodeText.setClickable(true);
                tvChangePwdCodeText.setTextColor(ContextCompat.getColor(mContext, R.color.text_primary));
            }
        }
    }

    @Override
    public void showCodeOn() {
        LoadingDialogUtils.dismissDialog_ios();
        if (mType == 3) {
            if (tvZfbCodeText != null) {
                tvZfbCodeText.setClickable(false);
                tvZfbCodeText.setTextColor(ContextCompat.getColor(mContext, R.color.color_999));
            }
        } else if (mType == 4) {
            if (tvChangePhoneGetcode != null) {
                tvChangePhoneGetcode.setClickable(false);
                tvChangePhoneGetcode.setTextColor(ContextCompat.getColor(mContext, R.color.color_999));
            }
        } else if (mType == 5) {
            if (tvChangePwdCodeText != null) {
                tvChangePwdCodeText.setClickable(false);
                tvChangePwdCodeText.setTextColor(ContextCompat.getColor(mContext, R.color.color_999));
            }
        }
    }

    @Override
    public void bindAlipaySuccess() {
        //绑定支付宝成功
        LoadingDialogUtils.dismissDialog_ios();
        StyleableToast.makeText(mContext, "绑定成功", Toast.LENGTH_SHORT, R.style.mytoast).show();
//        if (userInfoResponBean != null) {
//            userInfoResponBean.setIs_alipay(1);
//            realmHelper.insertUserInfoResponBean(userInfoResponBean);
//        }
        RxBus.getDefault().post(new SendEvent("", Constants.TYPE_GET_USERINFO));
        RxBus.getDefault().post(new SendEvent("", Constants.TYPE_ALIPAY_INFO));
        onBackPressedSupport();
    }

    @Override
    public void mobileEditSuccess() {
        //修改手机号成功
        LoadingDialogUtils.dismissDialog_ios();
        StyleableToast.makeText(mContext, "修改成功", Toast.LENGTH_SHORT, R.style.mytoast).show();
        RxBus.getDefault().post(new SendEvent("", Constants.TYPE_GET_USERINFO));
        onBackPressedSupport();
    }

    @Override
    public void editpwdSuccess() {
        //修改密码成功
        LoadingDialogUtils.dismissDialog_ios();
        StyleableToast.makeText(mContext, "修改成功", Toast.LENGTH_SHORT, R.style.mytoast).show();
        RxBus.getDefault().post(new SendEvent("", Constants.TYPE_GET_USERINFO));
        onBackPressedSupport();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.stopInterval();
        }
    }
}
